package com.maserb.monitor.web.jw_our;


import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwEducationClobEntity;
import com.maserb.monitor.entity.jwOur.JwOurEntity;
import com.maserb.monitor.entity.jwOur.JwOurLeaderEntity;
import com.maserb.monitor.enums.JwOurLeaderTypeEnum;
import com.maserb.monitor.enums.JwOurTypeEnum;
import com.maserb.monitor.service.jwOur.JwOurLeaderService;
import com.maserb.monitor.service.jwOur.JwOurService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

//关于我们
@Controller
@RequestMapping("/jwOurLeader")
public class JwOurLeaderController {
    @Autowired
    private JwOurLeaderService service;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "jw_our/indexLeader";
    }

    @RequestMapping("/doListOurLeader")
    @ResponseBody
    public PageResponseResult<JwOurLeaderEntity> doListOurLeader(PageRequestParam page) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<JwOurLeaderEntity>>() {
            @Override
            public Page<JwOurLeaderEntity> invoke(Pageable var1) throws Exception {
                Page<JwOurLeaderEntity> page = service.findAll(var1);
                for(JwOurLeaderEntity entity:page.getContent()){
                    if(entity.getRank().equals("1")){
                        entity.setRankCn("书记");
                    }else if(entity.getRank().equals("2")){
                        entity.setRankCn("副书记");
                    }else {
                        entity.setRankCn("监察委员");
                    }
                }
                return page;
            }
        });
    }


    @RequestMapping("/getAddOurLeader")
    public ModelAndView getAddOurLeader(){
        ModelAndView view = new ModelAndView();
        view.setViewName("jw_our/our_leader_add");
        JwOurLeaderTypeEnum[] infoType = JwOurLeaderTypeEnum.values();
        view.addObject("infoType",infoType);
        return view;
    }

    @RequestMapping("/doAddOurLeader")
    @ResponseBody
    public AjaxResult<Boolean> doAddOurLeader(JwOurLeaderEntity entity,String clobValues) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                service.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateOurLeader/{id}")
    public ModelAndView getUpdateOurLeader(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        JwOurLeaderEntity entity = service.findOne(id);
        result.setViewName("jw_our/our_leader_edit");
        JwOurLeaderTypeEnum[] infoType = JwOurLeaderTypeEnum.values();
        result.addObject("infoType",infoType);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateOurLeader")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateOurLeader(final HttpServletRequest request, JwOurLeaderEntity entity, String clobValues) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                service.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteOurLeader")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteOurLeader(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                service.deleteAllByKeys(keys);
                return true;
            }
        });
    }


}
